package com.example.controller;

import com.example.dto.UserPageDto;
import com.example.entity.NewPassword;
import com.example.entity.Page;
import com.example.entity.Result;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public Result login(@RequestBody User user) {
        Result result = new Result();
        User login = userService.login(user);
        if (login != null) {
            login.setLastLoginTime(new Date());
            int i = userService.UpdateLastLoginTime(login);
            if (i > 0) {
                result.setCode(1);
                result.setMsg("登录成功!");
                List<User> userList = new ArrayList<>();
                userList.add(login);
                result.setData(userList);
            } else {
                result.setCode(0);
                result.setMsg("登录失败!");
            }
        } else {
            User userStateByAccount = userService.findUserStateByAccount(user);
            if (userStateByAccount != null) {
                if (userStateByAccount.getState() == 0) {
                    result.setCode(0);
                    result.setMsg("该账号已停用!");
                }
            } else {
                result.setCode(0);
                result.setMsg("账号、密码或角色错误!");
            }
        }
        return result;
    }

    @PostMapping("add")
    public Result addUser(@RequestBody User user) {
        Result result = new Result();
        if (user != null) {
            user.setLastLoginTime(new Date());
            user.setPassword(user.getAccount().substring(5, 11));
            user.setRegTime(new Date());
            user.setName(user.getAccount());
        }
        int i = userService.addUser(user);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("注册成功!");
        } else {
            result.setCode(0);
            result.setMsg("注册失败!");
        }
        return result;
    }

    @GetMapping("list")
    public Result findAllUser(Page page) {
        Result result = new Result();
        if (page != null) {
            int pageHelper = (page.getCurrentPage() - 1) * page.getPageSize();
            page.setCurrentPage(pageHelper);
        }
        List<User> userList = userService.findAllUser(page);
        List<User> count = userService.findAll();
        if (userList != null) {
            result.setCode(1);
            result.setMsg("查询成功!");
            result.setData(userList);
            result.setTotal(count.size());
        } else {
            result.setCode(0);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @PostMapping("edit")
    public Result editUser(@RequestBody User user) {
        Result result = new Result();
//        User userId = userService.findUserById(user.getUid());
//        if (user != null) {
//            if (user.getAccount() == null) {
//                user.setAccount(userId.getAccount());
//            }
//            if (user.getRole() == null) {
//                user.setRole(userId.getRole());
//            }
//        } else {
//            result.setCode(0);
//            result.setMsg("修改失败!");
//            return result;
//        }

        int i = userService.editUser(user);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("修改成功!");
        } else {
            result.setCode(0);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @PostMapping("editPassword")
    public Result editPassword(@RequestBody NewPassword password) {
        Result result = new Result();
        if (password != null) {
            User user = userService.findUserById(password.getUid());
            if (password.getOldPassword().equals(user.getPassword())) {
                int i = userService.editPassword(password);
                if (i > 0) {
                    result.setCode(1);
                    result.setMsg("密码修改成功!");
                    return result;
                }
            }
        }
        result.setCode(0);
        result.setMsg("密码修改失败!");
        return result;
    }

    @PostMapping("del")
    public Result delUser(@RequestBody User user) {
        Result result = new Result();
        // 删除用户方法1：通过字段state来设置用户是否删除
        int i = userService.delUser(user);
        // 删除用户方法2：直接在表里删除该用户的数据
//        int i = userService.deleteUser(user);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("禁用成功!");
        } else {
            result.setCode(0);
            result.setMsg("禁用失败!");
        }
        return result;
    }

    @PostMapping("resetPwd")
    public Result resetPwd(@RequestBody User user) {
        Result result = new Result();
        if (user != null) {
            int i = userService.resetPwd(user);
            if (i > 0) {
                result.setCode(1);
                result.setMsg("密码重置成功!");
                return result;
            }
        }
        result.setCode(0);
        result.setMsg("密码重置失败!");
        return result;
    }

    @PostMapping("search")
    public Result searchUser(@RequestBody UserPageDto userPageDto) {
        Result result = new Result();
        Page page = userPageDto.getPage();
        User user = userPageDto.getUser();

        if (page != null) {
            int pageHelper = (page.getCurrentPage() - 1) * page.getPageSize();
            page.setCurrentPage(pageHelper);
        }

        List<User> userList = userService.searchUser(page, user);
        List<User> allUser = userService.searchAllUser(user);
        if (userList != null) {
            result.setCode(1);
            result.setMsg("查询成功，有" + allUser.size() + "个结果!");
            result.setData(userList);
            result.setTotal(allUser.size());
        } else {
            result.setCode(0);
            result.setMsg("查询结果为空!");
        }
        return result;
    }

    @PostMapping("enable")
    public Result enableUser(@RequestBody User user) {
        Result result = new Result();
        int i = userService.enableUser(user);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("启用成功!");
        } else {
            result.setCode(0);
            result.setMsg("启用失败!");
        }
        return result;
    }

    @PostMapping("delById")
    public Result deleteUserById(@RequestBody int[] users) {
        Result result = new Result();
        int i = userService.deleteUsersById(users);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("删除成功!");
        } else {
            result.setCode(0);
            result.setMsg("删除失败!");
        }
        return result;
    }
}
